<template>
  <el-card style="margin-bottom:20px;">
    <div slot="header" class="clearfix">
      <span>{{ $t('profile.profile') }}</span>
    </div>

    <div class="user-profile">
      <div class="box-center">
        <pan-thumb :image="user.avatar" :height="'100px'" :width="'100px'" :hoverable="false">
          <div>{{ $t('profile.greet') }}</div>
          {{ user.role }}
        </pan-thumb>
      </div>
      <div class="box-center">
        <div class="user-name text-center">{{ user.name }}</div>
        <div class="user-role text-center text-muted">{{ user.role }}</div>
      </div>
    </div>

    <div class="user-bio">
      <div class="user-bio-section">
        <div class="user-bio-section-header"><svg-icon icon-class="peoples" /><span>{{ $t('profile.post') }}</span></div>
        <div class="user-bio-section-body">
          <div class="text-muted">
            {{ user.post }}
          </div>
        </div>
      </div>
      <div class="user-bio-section">
        <div class="user-bio-section-header">
          <i class="el-icon-info" />
          <el-tooltip class="item" effect="light" content="工号可作为用户登录账号，不可修改" placement="right">
            <span>{{ $t('profile.workId') }}</span>
          </el-tooltip>
        </div>
        <div class="user-bio-section-body">
          <div class="text-muted">
            {{ user.workId }}
          </div>
        </div>
      </div>
      <div class="user-bio-section">
        <div class="user-bio-section-header"><svg-icon icon-class="education" /><span>{{ $t('profile.education') }}</span></div>
        <div class="user-bio-section-body">
          <div class="text-muted">
            {{ user.college }} <svg-icon icon-class="edit" @click="editEducation" />
          </div>
        </div>
      </div>
      <div class="user-bio-section">
        <div class="user-bio-section-header"><i class="el-icon-location" /><span>{{ $t('profile.address') }}</span></div>
        <div class="user-bio-section-body">
          <div class="text-muted">
            {{ user.address }} <svg-icon icon-class="edit" @click="editAddress" />
          </div>
        </div>
      </div>

      <el-dialog :visible.sync="dialogEditEducationVisible" :title="$t('profile.editEducation')">
        <el-form
          label-position="left"
          label-width="100px"
          style="width: 400px; margin-left:50px;"
        >
          <el-form-item :label="$t('profile.nowEducation')" style="color: #0a76a4">
            {{ user.college }}
          </el-form-item>
          <el-form-item :label="$t('profile.newEducation')" prop="newCollege">
            <el-input v-model="newCollege" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="plain" @click="dialogEditEducationVisible = false">{{ $t('profile.cancel') }}</el-button>
          <el-button type="primary" @click="editEducationConfirm">{{ $t('profile.confirm') }}</el-button>
        </span>
      </el-dialog>

      <el-dialog :visible.sync="dialogEditAddressVisible" :title="$t('profile.editAddress')">
        <el-form
          label-position="left"
          label-width="100px"
          style="width: 400px; margin-left:50px;"
        >
          <el-form-item :label="$t('profile.nowAddress')" style="color: #0a76a4">
            {{ user.address }}
          </el-form-item>
          <el-form-item :label="$t('profile.newAddress')" prop="newAddress">
            <el-input v-model="newAddress" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="plain" @click="dialogEditAddressVisible = false">{{ $t('profile.cancel') }}</el-button>
          <el-button type="primary" @click="editAddressConfirm">{{ $t('profile.confirm') }}</el-button>
        </span>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import PanThumb from '@/components/PanThumb'
import { editAddressInfo, editEducationInfo } from '@/api/user'

export default {
  components: { PanThumb },
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          id: 0,
          name: '',
          email: '',
          avatar: '',
          role: '',
          post: '',
          college: '',
          workId: '',
          address: ''
        }
      }
    }
  },
  data: function() {
    return {
      dialogEditEducationVisible: false,
      newCollege: '',
      dialogEditAddressVisible: false,
      newAddress: ''
    }
  },
  methods: {
    editEducation() {
      this.dialogEditEducationVisible = true
    },
    editEducationConfirm() {
      const temp = { workId: this.user.workId, newCollege: this.newCollege }
      // 根据修改返回的数据，将页面信息修改
      editEducationInfo(temp).then(response => {
        if (response.data === 1) {
          console.log(response.data)
          this.user.college = this.newCollege
          this.newCollege = ''
          this.$parent.$message({
            message: '操作成功',
            type: 'success'
          })
        }
      })
      this.dialogEditEducationVisible = false
    },
    editAddress() {
      this.dialogEditAddressVisible = true
    },
    editAddressConfirm() {
      const temp = { workId: this.user.workId, newAddress: this.newAddress }
      // 根据修改返回的数据，将页面信息修改
      editAddressInfo(temp).then(response => {
        if (response.data === 1) {
          console.log(response.data)
          this.user.address = this.newAddress
          this.newAddress = ''
          this.$parent.$message({
            message: '操作成功',
            type: 'success'
          })
        }
      })
      this.dialogEditAddressVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
</style>
